package cn.kgc.service.impl;

import cn.kgc.entities.Emp;
import cn.kgc.mapper.EmpMapper;
import cn.kgc.service.EmpService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpMapper empMapper;

    @Override
    public List<Emp> getAllEmp() {
        //分页
        PageHelper.startPage(2,2);
        return empMapper.selectAll();
    }

    @Override
    public Emp getEmpById(Integer id) {
        return empMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addEmp(Emp emp) {
        empMapper.insert(emp);
    }

    @Override
    public void updateEmp(Emp emp) {
        empMapper.updateByPrimaryKey(emp);
    }

    @Override
    public void deleteEmpById(Integer id) {
        empMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer countByName(String name) {
        /*return empMapper.countByName(name);*/
        /*Emp param = new Emp();
        param.setName(name);

        List<Emp> list = empMapper.select(param);
        System.out.println(list);
        return list.size();*/
        Example example = new Example(Emp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name",name);

        List<Emp> emps = empMapper.selectByExample(example);
        System.out.println(emps);
        return emps.size();


    }
}
